#pragma once
#include<assert.h>
#include<stdio.h>
#include<string.h>
using namespace std;
namespace bit
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			memmove(_str, str, _size + 1);
		}
		string(const string& str)
		{
			_size = str._size;
			_capacity = str._capacity;
			_str = new char[_capacity + 1];
			memmove(_str, str._str, _size + 1);
		}
		~string()
		{
			if (_str != nullptr)
			{
				delete[] _str;
				_str = nullptr;
			}
			_capacity = 0;
			_size = 0;
		}
		const char* c_str()const
		{
			return this->_str;
		}
		size_t size()const
		{
			return _size;
		}
		const char& operator[](size_t pos)const
		{
			assert(pos < _size);
			return _str[pos];
		}
		char* begin()const
		{
			return _str;
		}
		char* end()const
		{
			return _str + _size;
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tem = new char[n + 1];
				memmove(tem, _str, _size + 1);
				delete[] _str;
				_capacity = n;
				_str = tem;
			}
		}
		void insert(size_t pos, size_t n, const char c)
		{
			assert(pos <= _size);
			if (_size + n  > _capacity)
			{
				reserve(n + _size);
			}
			int end = (int)_size;
			while (end >= (int)pos)
			{
				_str[end + n] = _str[end];
				end--;
			}
			for (int i = n; i > 0; i--)
			{
				_str[pos + i - 1] = c;
			}
			_size += n;
		}
		void resize(size_t n, const char c='\0')
		{
			if (n > _size && n > _capacity)
			{
				reserve(n);
				int k = n - _size;
				for (int i = _size; i < _size + k; i++)
					{
						_str[i] = c;
					}
				_str[_size + k] = '\0';
				_capacity = n;
				_size = n;
			}
			if (n > _size && n < _capacity)
			{
				int k = n - _size;
				for (int i = _size; i < _size + k; i++)
				{
					_str[i] = c;
				}
				_str[_size+k] = '\0';
				_size = n;
			}
			if (n < _size && n < _capacity)
			{
				_str[n] = '\0';
				_size = n;
			}
		}
		void push_back(char c)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			_str[_size] = c;
			++_size;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			int size = strlen(str);
			if (_size + size > _capacity)
			reserve(_size + size+1);
			memmove(_str + _size, str, size+1);
			_size += size;
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator=(const string& str)
		{
			_size = str._size;
			_capacity = str._capacity;
			_str = new char[_capacity + 1];
			memmove(_str, str._str, _size + 1);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
		void erase(size_t pos, size_t len = (size_t)-1)
		{
			assert(pos <= _size);
			if (len == (size_t)-1||pos+len>_size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				int end = pos;
				while (end+len<=_size)
				{
					_str[end] = _str[end + len];
					end++;
				}
				_str[end] = '\0';
				_size -= len;
			}
		}
		size_t find(char ch, size_t pos = 0)
		{
			assert(pos <= _size);
			for (int i = pos; i <= _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}
			return (size_t)-1;
		}
		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos <= _size);
			const char* tem = strstr(_str + pos, str);
			if (tem)
			{
				return tem-_str;
			}
			else
			{
				return (size_t)-1;
			}
		}
		string substr(size_t pos = 0, size_t len = (size_t)-1)
		{
			assert(pos < _size);
			bit::string a;
			if (len == (size_t)-1||len+pos>_size)
			{
				a.reserve(_size - pos);
				a._capacity = _size - pos;
				for (int i = pos; i < _size; i++)
				{
					a += _str[i];
				}
			}
			else
			{
				a.reserve(len);
				a._capacity = len;
				for (int i = pos; i < pos + len; i++)
				{
					a += _str[i];
				}
			}
			a._size = len;
			return a;
		}
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
		bool operator<(const bit::string& s) const
		{
			int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);
			return ret == 0 ? _size < s._size : ret < 0;
		}

		bool operator==(const bit::string& s) const
		{
			return _size == s._size&& memcmp(_str, s._str, _size) == 0;
		}

		bool operator<=(const bit::string& s) const
		{
			return *this < s || *this == s;
		}

		bool operator>(const bit::string& s) const
		{
			return !(*this <= s);
		}

		bool operator>=(const bit::string& s) const
		{
			return !(*this < s);
		}

		bool operator!=(const bit::string& s) const
		{
			return !(*this == s);
		}
		const static size_t npos = -1;
	private:
		size_t _size;
		size_t _capacity;
		char* _str;
	};
	ostream& operator<<(ostream& out, const bit::string& str)
	{
		for (auto s : str)
		{
			out << s;
		}
		return out;
	}
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch = in.get();
		while (ch == ' ' || ch == '\n')
		{
			ch = in.get();
		}
		char b[128];
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			b[i++] = ch;
			if (i == 127)
			{
				b[i] = '\0';
				s += b;
				i = 0;
			}
			ch = in.get();
		}
		if (i != 0)
		{
			b[i] = '\0';
			s += b;
		}
		return in;
	}
};